package com.seio.edc.controller.statistics;

import com.nova.edc.model.system.CustomerInfo;
import com.nova.edc.model.system.Login;
import com.nova.edc.model.system.ModelConstant;
import com.nova.edc.model.util.CountModel;
import com.nova.edc.service.energy.BillStatisticsService;
import com.nova.edc.service.energy.EnergyService;
import com.nova.edc.service.energy.GatherSummaryService;
import com.nova.edc.service.energy.SummaryService;
import com.nova.edc.service.system.CustomerInfoService;
import com.nova.frame.dao.pager.Page;
import com.nova.frame.dao.pager.PageList;
import com.seio.edc.util.DateUtil;
import com.seio.edc.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/3/9 0009.
 */
@Controller
@RequestMapping(value = "/count")
public class CountController {

    @Autowired
    private CustomerInfoService novaCustomerInfoService;
    @Autowired
    private BillStatisticsService novaBillStatisticsService;
    @Autowired
    private EnergyService novaEnergyService;
    @Autowired
    private SummaryService novaSummaryService;
    @Autowired
    private GatherSummaryService novaGatherSummaryService;

    private static final int PAGE_SIZE = 5;

    @RequestMapping(value = "/countOne")
    public ModelAndView countOne(HttpServletRequest request,
                                 HttpSession session, ModelMap modelMap) {
        Login login = (Login) session.getAttribute("sessionLogin");
        if (login.getRoletype() == null || login.getRoletype() != ModelConstant.ROLE_GROUP) {
            CustomerInfo customerInfo = novaCustomerInfoService.getById(login.getCustomerinfoid());
            modelMap.put("customerInfoData", customerInfo);
        }
        return new ModelAndView("statistics/countone", modelMap);
    }

    @RequestMapping(value = "/countMany")
    public ModelAndView countMany(HttpServletRequest request,
                                  HttpSession session, ModelMap modelMap) {
        List<String> list = novaCustomerInfoService.findAllProvince();
        modelMap.put("provinces", list);
        modelMap.put("personel", ModelConstant.PERSONEL_SELECT);
        modelMap.put("acreage", ModelConstant.ACREAGE_SELECT);
        return new ModelAndView("statistics/countMany", modelMap);
    }

    @RequestMapping(value = "/countonepreview")
    public ModelAndView countonepreview(HttpServletRequest request,
                                        HttpSession session, ModelMap modelMap, Integer customerinfoid, String startDate, String endDate) {
        List<String> list = novaCustomerInfoService.findAllProvince();
        modelMap.put("customerinfoid", customerinfoid);
        modelMap.put("startDate", startDate);
        modelMap.put("endDate", endDate);
        return new ModelAndView("statistics/countonepreview", modelMap);
    }

    @RequestMapping(value = "/countmanypreview")
    public ModelAndView countmanypreview(HttpServletRequest request,
                                         HttpSession session, ModelMap modelMap, CountModel countModel) {
        modelMap.put("countModel", countModel);
        return new ModelAndView("statistics/countmanypreview", modelMap);
    }

    @RequestMapping(value = "getArea.shtml")
    @ResponseBody
    public List<String> getArea(
            HttpServletRequest request, HttpSession session,
            String area, String type) {
        List<String> list = null;
        if ("city".equals(type)) {
            list = novaCustomerInfoService.findCitysByProvince(area);
        } else {
            list = novaCustomerInfoService.findDistrictsByCity(area);
        }
        return list;
    }

    @RequestMapping(value = "/excuteCountManyAjax.shtml")
    @ResponseBody
    public List<Map<String, Object>> excuteCountManyAjax(HttpServletRequest request, HttpSession session,
                                                         ModelMap modelMap, CountModel countModel) {
        Login login = (Login) session.getAttribute("sessionLogin");
        if (StringUtil.isEmpty(countModel.getStartDate())) {
            countModel.setStartDate(DateUtil.getAddDate(DateUtil.DEFAULT_DAY, -7,
                    DateUtil.DEFAULT_DATE_FORMAT));
        }
        if (StringUtil.isEmpty(countModel.getEndDate())) {
            countModel.setEndDate(DateUtil.today(DateUtil.DEFAULT_DATE_FORMAT));
        }
        Page page = new Page();
        page.setCurrentPage(1);
        page.setPageSize(PAGE_SIZE);
        modelMap.put("pageIndex", page.getCurrentPage());
        PageList<CustomerInfo> countModelPagelist = novaCustomerInfoService.findByCountModelPagelist(page, countModel, login);
        List<CustomerInfo> customerInfoList = countModelPagelist.getDatas();
        List<Map<String, Object>> mapList = novaGatherSummaryService.getStatisticsByDayCuscomer(countModel, customerInfoList);
        return mapList;
    }

    @RequestMapping(value = "/getCustomer.shtml")
    @ResponseBody
    public CustomerInfo getCustomer(
            HttpServletRequest request, HttpSession session, Integer companyCode) {
        Login login = (Login) session.getAttribute("sessionLogin");
        if (companyCode == null || companyCode == 0) {
            companyCode = novaCustomerInfoService.getById(login.getCustomerinfoid()).getCompanycode();
        }
        CustomerInfo customerInfo = novaCustomerInfoService.getByCompanyCode(companyCode);
        return customerInfo;
    }

    // 获取某个单位每天的耗电量
    @RequestMapping(value = "/findPowerEveryDay.shtml")
    @ResponseBody
    public List<Map<String, Object>> findPowerEveryDay(
            HttpServletRequest request, HttpSession session,
            Integer customerinfoid, String startDate, String endDate) {
        Login login = (Login) session.getAttribute("sessionLogin");
        if (StringUtil.isEmpty(startDate)) {
            startDate = DateUtil.getAddDate(DateUtil.DEFAULT_DAY, -7,
                    DateUtil.DEFAULT_DATE_FORMAT);
        }

        if (StringUtil.isEmpty(endDate)) {
            endDate = DateUtil.today(DateUtil.DEFAULT_DATE_FORMAT);
        }
        if (customerinfoid == null || customerinfoid == 0) {
            customerinfoid = login.getCustomerinfoid();
        }
        CustomerInfo customerInfo = novaCustomerInfoService.getById(customerinfoid);
        List<Map<String, Object>> list = novaGatherSummaryService.getEnergyByDate(
                customerinfoid, startDate, endDate);

        if (list.size() == 0) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("statisticsdate", startDate);
            map.put("customername", customerInfo.getCustomername());
            Map<String, Object> map1 = new HashMap<String, Object>();
            map1.put("statisticsdate", endDate);
            map1.put("customername", customerInfo.getCustomername());
            list.add(0, map);
            list.add(1, map1);
        }
        return list;
    }

    // 获取某个单位一段时间内每个区域的耗电量
    @RequestMapping(value = "/findPowerEveryRegion.shtml")
    @ResponseBody
    public List<Map<String, Object>> findPowerEveryRegion(
            HttpServletRequest request, HttpSession session,
            Integer customerinfoid, String startDate, String endDate) {
        Login login = (Login) session.getAttribute("sessionLogin");
        if (StringUtil.isEmpty(startDate)) {
            startDate = DateUtil.getAddDate(DateUtil.DEFAULT_DAY, -7,
                    DateUtil.DEFAULT_DATE_FORMAT);
        }

        if (StringUtil.isEmpty(endDate)) {
            endDate = DateUtil.today(DateUtil.DEFAULT_DATE_FORMAT);
        }
        if (customerinfoid == null || customerinfoid == 0) {
            customerinfoid = login.getCustomerinfoid();
        }
        List<Map<String, Object>> list = novaGatherSummaryService.getEnergyByRegion(
                customerinfoid, startDate, endDate);
        CustomerInfo customerInfo = novaCustomerInfoService.getById(customerinfoid);
        if (list.size() == 0) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("statisticsdate", startDate);
            map.put("customername", customerInfo.getCustomername());
            Map<String, Object> map1 = new HashMap<String, Object>();
            map1.put("statisticsdate", endDate);
            map1.put("customername", customerInfo.getCustomername());
            list.add(0, map);
            list.add(1, map1);
            list.get(0).put("position", "暂无数据 ");
        } else {
            int position = novaGatherSummaryService.positionInProvince(customerInfo, startDate, endDate);
            if (position == 0) {
                list.get(0).put("position", position);
            } else {
                list.get(0).put("position", position);
            }
        }
        return list;
    }
}
